package com.learning.server.design;

/**
 * 解释器模式是我们暂时的最后一讲，一般主要应用在OOP开发中的编译器的开发中，所以适用面比较窄。
 */

interface Expression {
    public int interpret(IContext context);
}

class IPlus implements Expression {

    @Override
    public int interpret(IContext context) {
        return context.getNum1()+context.getNum2();
    }
}

class IMinus implements Expression {

    @Override
    public int interpret(IContext context) {
        return context.getNum1()-context.getNum2();
    }
}

class IContext {

    private int num1;
    private int num2;

    public IContext(int num1, int num2) {
        this.num1 = num1;
        this.num2 = num2;
    }

    public int getNum1() {
        return num1;
    }
    public void setNum1(int num1) {
        this.num1 = num1;
    }
    public int getNum2() {
        return num2;
    }
    public void setNum2(int num2) {
        this.num2 = num2;
    }


}


public class Interpreter {

    public static void main(String[] args) {

        // 计算9+2-8的值
        int result = new IMinus().interpret((new IContext(new IPlus()
                .interpret(new IContext(9, 2)), 8)));
        System.out.println(result);
    }

}
